package com.kangkang.leetcode20;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Test274 {
    public static void main(String[] args) {
        int[] a = {3,0,6,1,5};
        System.out.println(Solution274.hIndex(a));
    }
}
class Solution274 {
    public static int hIndex(int[] citations) {
        Arrays.sort(citations);
        for (int i = citations.length-1; i >= 0; i--) {
            if(i == 0){
                return Math.min(citations[i], citations.length);
            }
            if(citations[i] != citations[i-1]){
                for (int j = citations[i];j>citations[i-1];j--){
                    if(citations.length-i >= j){
                        return j;
                    }
                }
            }
        }
        return 0;
    }
    public static int hIndex1(int[] citations) {
        int n = citations.length;
        int total = 0;
        int[] counter = new int[n+1];
        for (int citation : citations) {
            if (citation >= n) {
                counter[n]++;
            } else {
                counter[citation]++;
            }
        }
        for (int i = n; i >= 0; i--) {
            total += counter[i];//大于等于  i  的论文总数
            if(total > i){//如果 大于等于  i  的论文总数 大于等于 i
                return i;
            }
        }
        return 0;

    }
}